﻿// importOCX.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include <io.h>
#include <shlwapi.h>
#include "nt120client.tlh"
#pragma comment(lib,"shlwapi.lib")

using namespace NT120Client;

void RegisterActiveX() {
	/*CLSID lpclsid;
	HRESULT hr = ::CLSIDFromProgID(OLESTR("NT120Client.NT120ClientAPI"), &lpclsid);
	if (SUCCEEDED(hr)) {
		INT120ClientAPI* clientAPI = NULL;
		hr = ::CoInitialize(NULL);
		hr = ::CoCreateInstance(lpclsid, 0, CLSCTX_ALL, __uuidof(INT120ClientAPI), (void**)&clientAPI);
		_bstr_t random = _bstr_t((const char*)"xxuoftfcnvgmkxgnnxbx");
		long res = clientAPI->NTFind();
		res = clientAPI->NTLogin(_bstr_t("1111"));
		_bstr_t encryptStr = clientAPI->NTMD5(random);
		std::cout << "encrypt:" << encryptStr << "\n";

		return;
	}*/
	HRESULT(STDAPICALLTYPE * pfnDllGetClassObject)(REFCLSID rclsid, REFIID riid, LPVOID * ppv) = 0;
	char fullPath[MAX_PATH] = { 0 };
	SetCurrentDirectory("D:\\Program Files\\360SE\\360Chrome\\Chrome\\User Data\\Default\\Extensions\\jhkcgajlpemjffimcnfnadbaeangkfdh\\1.0_0");
	LPCSTR modulePath = _fullpath(fullPath, "./NT120Client.dll", MAX_PATH);
	if (modulePath != NULL) {
		HMODULE pluginModule = ::LoadLibraryEx(fullPath, NULL, 0);
		if (pluginModule != NULL) {
			*(LPVOID*)&pfnDllGetClassObject = ::GetProcAddress(pluginModule, "DllGetClassObject");
			if (pfnDllGetClassObject != NULL) {
				IClassFactoryPtr pClassFactory = NULL;
				HRESULT hr = pfnDllGetClassObject(__uuidof(NT120ClientAPI), IID_IClassFactory, (LPVOID*)&pClassFactory);
				if (SUCCEEDED(hr) && pClassFactory != nullptr){
					INT120ClientAPIPtr clientAPI;
					hr = pClassFactory->CreateInstance(NULL, __uuidof(INT120ClientAPI), (void**)&clientAPI);
					if (SUCCEEDED(hr) && clientAPI != nullptr){
						long res = clientAPI->NTFind();
						if (res == 0) {
							_bstr_t keyNumber = clientAPI->NTGetHardwareID();
							std::cout << "不注册调用：" << keyNumber;
							clientAPI->Release();
							clientAPI = NULL;
						}
					}
				}
			}
		}
	}


	/*HINSTANCE hLib = LoadLibrary(modulePath);
	if (hLib != NULL) {
		FARPROC lpDllEntryPoint = GetProcAddress(hLib, __TEXT("DllRegisterServer"));
		if (lpDllEntryPoint != NULL)
		{
			if (FAILED((*lpDllEntryPoint)()))
			{
				FreeLibrary(hLib);
			}
		}
	}*/
	//if (modulePath != NULL && _access(modulePath, 0)) {
	//	//释放资源文件中的dll
	//	HMODULE hModule = GetModuleHandle(NULL);
	//	HANDLE dllFile = CreateFile(modulePath, GENERIC_ALL, FILE_SHARE_WRITE || FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	//	HRSRC hrsc = FindResource(hModule, "IDR_DLL", "dll");
	//	if (hrsc != NULL) {
	//		HGLOBAL dllRes = LoadResource(NULL, hrsc);
	//		if (dllRes != NULL) {
	//			LPVOID pData = LockResource(dllRes);
	//			DWORD dwSize = SizeofResource(hModule, hrsc);
	//			FILE* fp = NULL;
	//			SECURITY_ATTRIBUTES sec;
	//			errno_t err = fopen_s(&fp, (const char*)modulePath, "w");
	//			if (fp != NULL) {
	//				fwrite((char*)pData, 1, dwSize, fp);
	//				fclose(fp);
	//				UnlockResource(pData);

	//				HINSTANCE hLib = LoadLibrary(modulePath);
	//				if (hLib != NULL) {
	//					FARPROC lpDllEntryPoint = GetProcAddress(hLib, __TEXT("DllRegisterServer"));
	//					if (lpDllEntryPoint != NULL)
	//					{
	//						if (FAILED((*lpDllEntryPoint)()))
	//						{
	//							FreeLibrary(hLib);
	//						}
	//					}
	//				}
	//			}
	//		}
	//	}
	//}
}

int main()
{
	std::cout << "Hello World!\n";
	IDispatch* pIDispatch = NULL;
	INT120ClientAPI* clientAPI = NULL;
	CLSID clsid;
	IID iid;

	RegisterActiveX();

	HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if (SUCCEEDED(hr)) {
		hr = ::CLSIDFromProgID(OLESTR("NT120Client.NT120ClientAPI"), &clsid);
		if (SUCCEEDED(hr)) {
			hr = ::CoCreateInstance(clsid, 0, CLSCTX_ALL, __uuidof(INT120ClientAPI), (void**)&clientAPI);
			if (SUCCEEDED(hr)) {
				long findRes = clientAPI->NTFind();
				if (findRes == 0) {
					_bstr_t keyNumber = clientAPI->NTGetHardwareID();
					std::cout << keyNumber;
				}
				else {
					std::cout << "key not found!";
				}

			}
		}
	}
	std::cout << std::getchar();
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
